package com.kite.music;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;

import com.kite.ossServer.OSS;

import java.security.SecureRandom;

public class JavaUtil{

    private  final String DEFAULT_TAG = "KiteMusic_JavaUtil";
    //日志默认开启
    private boolean isDebugMode = true;

    private WebView webview;
    private Activity activity;
    private Context context;
    private ClipboardManager clipboardManager;
    private Vibrator vibrator;

    public JavaUtil(WebView webview, Activity activity, Context context){
        this.webview = webview;
        this.activity = activity;
        this.context = context;
        clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    }

    // 随机数生成 生成126bit随机数
    // @name generateRandom()
    // @param void
    // return String
    public static String generateRandom() {
        try {
            SecureRandom secureRandom = new SecureRandom();
            StringBuilder randomNum = new StringBuilder(126);
            for (int i = 0; i < 126; i++) {
                randomNum.append(secureRandom.nextInt(2));
            }
            //...
            return randomNum.toString();
        } catch (Exception e) {
            //...
            return "";
        }
    }

    // 剪切板操作：复制内容到系统剪切板
    // @name copyToClipboard
    // @param content 要复制的文本内容
    // @return boolean 复制成功返回true，失败返回false
    @JavascriptInterface
    public boolean copyToClipboard(String content) {
        if (content == null || content.isEmpty()) {
            //...
            return false;
        }
        try {
            ClipData clipData = ClipData.newPlainText("KiteMusic_Copy", content);
            clipboardManager.setPrimaryClip(clipData);
            //...
            return true;
        } catch (Exception e) {
            //...
            return false;
        }
    }

    // 震动功能 支持Android8及以上
    // @name vibrate
    // @param millis 震动时长（毫秒）
    // @return 是否震动成功
    @JavascriptInterface
    public boolean vibrate(long millis) {
        if (millis <= 0) {
            //...
            return false;
        }
        try {
            VibrationEffect vibrationEffect = VibrationEffect.createOneShot(millis, VibrationEffect.DEFAULT_AMPLITUDE);
            vibrator.vibrate(vibrationEffect);
            //...
            return true;
        } catch (Exception e) {
            //...
            return false;
        }
    }

    @JavascriptInterface
    public OSS requestOSS(String db_name) throws Exception {
//        return OSS.creatOSS(db_name);
        return new OSS(db_name);
    }

    // 测试
    @JavascriptInterface
    public boolean test(){
        return true;
    }
}